package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.repository.ApAssociateWordsRepository;
import com.heima.search.service.IApAssociateWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: itheima
 * @create: 2022-01-05 11:46
 */
@Service
public class ApAssociateWordsServiceImpl implements IApAssociateWordsService {

    @Autowired
    private ApAssociateWordsRepository associateWordsRepository;


    /**
     * 根据用户传入关键字，查询存储到MongoDB中高频的联想词，查询前五条
     * @param dto
     * @return
     */
    @Override
    public ResponseResult search(ApArticleSearchDto dto) {
        //1.调用MongoDB持久层完成查询
        List<ApAssociateWords> list = associateWordsRepository.findTop5ByAssociateWordsContainingOrderByCreatedTime(dto.getSearchWords());
        return ResponseResult.okResult(list);
    }
}
